package com.shengyuan.manage_os.old.ying.erpm.controls;

import com.baidu.location.BDLocation;
import com.shengyuan.manage_os.im.common.utils.ToastUtil;
import com.shengyuan.manage_os.old.ying.erpm.MainApplication;
import android.content.Context;
import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils.TruncateAt;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class GpsButton extends LinearLayout implements OnClickListener {
	public static BDLocation pAMapLocation;
	public static boolean HasSend = false;
	public static long Time;
	public static String mlocation = "";
	public static double x, y ;
	
	public final static String LOCATION = "无位置数据";
	private LayoutParams PlayerBtnLayoutParams,DeleteBtnLayoutParams,TitleLayoutParams;
	public Button player, delete;
	public static TextView title;
	private String strLocation; 
	private GpsShowThread thread; 
	private ShowHandler pHandler;
	public String hint = "必须获取当前定位位置";
	public static final int ID_PLAYER = 31,  ID_DELETE = 32, ID_TITLE = 33, IDGPS = 34;
	
	@Override
	public void onClick(View v) {
		
		switch (v.getId()) {
		case ID_PLAYER:
			//MainApplication.currentActivity.pHandler.sendEmptyMessage(ID_PLAYER);
			MainApplication.currentActivity.pHandler.sendEmptyMessage(ID_PLAYER);
			return;
		case ID_TITLE:
			MainApplication.currentActivity.pHandler.sendEmptyMessage(ID_PLAYER);
			break;
		}
	}

	public GpsButton(Context context) {
		super(context);
		init(context);
	}

	public GpsButton(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);
	}


	private void init(Context context) {
		player = new Button(context);
		//delete = new Button(context);
		title = new TextView(context);
		player.setId(ID_PLAYER);
		//delete.setId(ID_DELETE);
		title.setId(ID_TITLE);
		PlayerBtnLayoutParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.MATCH_PARENT);
		//DeleteBtnLayoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.MATCH_PARENT);
		TitleLayoutParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.MATCH_PARENT);
		//DeleteBtnLayoutParams.weight =  PlayerBtnLayoutParams.weight = 1;
		TitleLayoutParams.weight= 5;
		
		addView(title, TitleLayoutParams);
		addView(player , PlayerBtnLayoutParams);
		//addView(delete, DeleteBtnLayoutParams);
		title.setHint( LOCATION );
		title.setTextSize(18.0f);
		title.setTextColor(Color.BLUE);
		title.setEllipsize(TruncateAt.MIDDLE);  
		title.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);  
		title.setSingleLine(true);  
		player.setText("地图定位");
		//delete.setText("删除");
		
		player.setOnClickListener(this);
		title.setOnClickListener(this);
		//delete.setOnClickListener(this);
		//VideoFileName = FileName;
		pHandler = new ShowHandler();
		thread = new GpsShowThread();
		thread.start();
	}
	private class GpsShowThread extends Thread {
		public boolean runing = true ;
		@Override
		public void run() {
			while(runing)
			{
				try {
					Thread.sleep(60000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				if (pAMapLocation!=null) {
					pHandler.sendEmptyMessage(0);
				}
			}
		}
	}
	
	public static class ShowHandler extends Handler {
		@Override
		public void handleMessage(Message msg) {
			try {
				if (pAMapLocation==null) {
					return;
				}
				if (pAMapLocation!= null) {
						x = pAMapLocation.getLatitude() ;
						y = pAMapLocation.getLongitude();
						String s = pAMapLocation.getAddrStr();
						if ( s==null || s.equals("") || s.trim().length()==0 ) {
							s =x +","+ y +"";
						}
					mlocation = pAMapLocation.getAddrStr();
				}
			} catch (Exception e) {
				System.out.println("#############pAMapLocation.getAddrStr()!!!Exception====="+e.toString());
				e.printStackTrace();
				title.setText(e.getMessage());
			}

		}
	}

}
